package com.demo.action;

import java.io.UnsupportedEncodingException; 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.demo.bean.Token;
import com.demo.entity.Useradmin;
import com.demo.service.UseradminService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
 
@RestController
@Api(tags = "帐号接口")
public class UserController extends BaseController {

	@Autowired
	private UseradminService us;
	private final static Logger logger = LoggerFactory.getLogger(UserController.class);

	@GetMapping(value = "login")
	public Object login(@ModelAttribute Useradmin useradmin, HttpSession session) throws Exception {
		if (useradmin != null && useradmin.getName() != null && useradmin.getPassword() != null) {
			// logger.info(useradmin.getName());
			Useradmin Useradmin = null;
			useradmin.setPassword(getSHA256(encrypByMd5Jar(useradmin.getPassword())));
			Useradmin = us.findUseradminByNameAndPassword(useradmin);
			if (Useradmin != null) {
				if (Useradmin.getPower() > 0) {
					Token token = new Token();
					token.setObj(Useradmin);
					token.setToken(encrypByMd5Jar(session.getId()));
				//	rs.expire(getSHA256(encrypByMd5Jar(session.getId())), Useradmin, 3600 * 3);
					return renderSuccess(token);
				} else {
					return renderError("帐号已被停用");
				}
			} else {
				return renderError("用户名或密码错误");
			}
		} else {
			return renderError("用户名或密码错误");
		}
	}


	public static String encrypByMd5Jar(String context) {
		return DigestUtils.md5DigestAsHex(context.getBytes());

	}

	public static String getSHA256(String str) {
		MessageDigest messageDigest;
		String encodestr = "";
		try {
			messageDigest = MessageDigest.getInstance("SHA-256");
			messageDigest.update(str.getBytes("UTF-8"));
			encodestr = byte2Hex(messageDigest.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return encodestr;
	}

	/**
	 * 将byte转为16进制
	 * 
	 * @param bytes
	 * @return
	 */
	private static String byte2Hex(byte[] bytes) {
		StringBuffer stringBuffer = new StringBuffer();
		String temp = null;
		for (int i = 0; i < bytes.length; i++) {
			temp = Integer.toHexString(bytes[i] & 0xFF);
			if (temp.length() == 1) {
				// 1得到一位的进行补0操作
				stringBuffer.append("0");
			}
			stringBuffer.append(temp);
		}
		return stringBuffer.toString();
	}

}
